<!DOCTYPE html>
<html>
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>首页</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="../../js/vue.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="../../js/jquery-3.3.1.min.js"></script>
    <style>
        html body{
            height: 100%;
            margin: 0;
            padding: 0;
        }
    </style>
</head>
<body>
<div id="app">
    <el-dialog
            title="用品信息"
            :visible.sync="dialogVisible"
            width="40%">
        <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="物品名称">
                <el-input v-model="form.goodsname"></el-input>
            </el-form-item>
            <el-form-item label="品牌">
                <el-input v-model="form.brand"></el-input>
            </el-form-item>
            <el-form-item label="规格">
                <el-input v-model="form.spec"></el-input>
            </el-form-item>
            <el-form-item label="物品数量">
                <el-input v-model="form.goodsCount"></el-input>
            </el-form-item>
            <el-form-item label="物品总金额">
                <el-input v-model="form.totalMoney"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="form.remark"></el-input>
            </el-form-item>
            <el-form-item label="用品分类">
                <el-select v-model="form.classifyId" placeholder="请选择">
                    <el-option
                            v-for="item in options"
                            :key="item.id"
                            :label="item.classname"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="add">提交</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <el-input v-model="name" style="width: 150px" placeholder="请输入物品名称"></el-input>
    <el-button type="primary" @click="select" plain>查询</el-button>
    <el-button type="primary" @click="dialogVisible = true;status=1" plain>添加</el-button>
    <el-table
            :data="tableData"
            style="width: 100%"
            max-height="500">
        <el-table-column
                fixed
                prop="ID"
                label="id"
                width="120">
        </el-table-column>
        <el-table-column
                prop="GOODSNAME"
                label="物品名称"
                width="120">
        </el-table-column>
        <el-table-column
                prop="BRAND"
                label="品牌"
                width="120">
        </el-table-column>
        <el-table-column
                prop="SPEC"
                label="规格"
                width="120">
        </el-table-column>
        <el-table-column
                prop="GOODS_COUNT"
                label="物品数量"
                width="120">
        </el-table-column>
        <el-table-column
                prop="TOTAL_MONEY"
                label="物品总金额"
                width="120">
        </el-table-column>
        <el-table-column
                prop="RECORD_DATE"
                label="登记日期"
                width="120">
            <template slot-scope="scope">
                {{ new Date(scope.row.RECORD_DATE).toLocaleDateString() }}
            </template>
        </el-table-column>
        <el-table-column
                prop="USERNAME"
                label="登记人"
                width="120">
        </el-table-column>
        <el-table-column
                prop="REMARK"
                label="备注"
                width="120">
        </el-table-column>
        <el-table-column
                prop="CLASSIFY_ID"
                label="用品分类"
                width="120">
            <template slot-scope="scope">
                {{scope.row.CLASSNAME}}
            </template>
        </el-table-column>
        </el-table-column>
        <el-table-column
                fixed="right"
                label="操作"
                width="120">
            <template slot-scope="scope">
                <el-button
                        @click.native.prevent="deleteRow(scope.$index, tableData)"
                        type="text"
                        size="small">
                    移除
                </el-button>
                <el-button
                        @click.native.prevent="updateRow(scope.$index, tableData)"
                        type="text"
                        size="small">
                    修改
                </el-button>
            </template>
        </el-table-column>
    </el-table>
</div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            name:"",
            dialogVisible: false,
            tableData:[],
            status:0,
            form: {
                goodsname: '',
                brand: '',
                spec: '',
                goodsCount: 0,
                totalMoney: 0,
                remark: '',
                classifyId: ''
            },
            options:[]
        },
        methods: {
            deleteRow(index, rows) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let vue= this;
                    $.ajax({
                        url:"http://localhost:8080/ssmzy05/goods/delete",
                        type:"post",
                        data:{
                          id:rows[index].ID
                        },
                        dataType:"json",
                        success:function (data) {
                            if(data==true){
                                alert("存在使用情况的用品不能删除")
                            }else {
                                vue.$message({
                                    type: 'success',
                                    message: '删除成功!'
                                });
                                rows.splice(index, 1);
                            }
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            updateRow(index, rows){
                let vue = this;
                vue.dialogVisible = true;
                vue.form.goodsname=rows[index].GOODSNAME;
                vue.form.brand=rows[index].BRAND;
                vue.form.spec=rows[index].SPEC;
                vue.form.goodsCount=rows[index].GOODS_COUNT;
                vue.form.totalMoney=rows[index].TOTAL_MONEY;
                vue.form.remark=rows[index].REMARK;
                vue.form.id=rows[index].ID;
                vue.form.classifyId=rows[index].CLASSIFY_ID;
                vue.status = 2;
            },
            select(){
                let vue= this;
                $.ajax({
                    url:"http://localhost:8080/ssmzy05/goods/select",
                    type:"post",
                    data:{
                      name:vue.name
                    },
                    dataType:"json",
                    success:function (data) {
                        vue.tableData=data;
                    }
                })
            },
            add(){
                let vue= this;
                vue.dialogVisible = false;
                if(vue.status==1){
                    $.ajax({
                        url:"http://localhost:8080/ssmzy05/goods/insert",
                        type:"post",
                        data: vue.form,
                        dataType:"json",
                        success:function (data) {
                            vue.select()
                        }
                    })
                }else {
                    $.ajax({
                        url:"http://localhost:8080/ssmzy05/goods/update",
                        type:"post",
                        data: vue.form,
                        dataType:"json",
                        success:function (data) {
                            vue.select()
                        }
                    })
                }
            },
            selectSelect(){
                let vue= this;
                $.ajax({
                    url:"http://localhost:8080/ssmzy05/classify/select",
                    type:"post",
                    data: vue.form,
                    dataType:"json",
                    success:function (data) {
                        vue.options=data;
                    }
                })
            }
        },
        mounted() {
            this.select();
            this.selectSelect();
        },
        watch: {
            dialogVisible(newValue) {
                if (!newValue) {
                    this.form = {
                        goodsname: '',
                        brand: '',
                        spec: '',
                        goodsCount: 0,
                        totalMoney: 0,
                        remark: '',
                        id: '',
                        classifyId: ''
                    };
                }
            }
        },
    })
</script>
</html>